********************************************************************************
* This do file:
*		1. Prepares data for firm-product level DD analysis (STEP 1)
* Last modified by: Jie Bai
* Date: 08/02/2017
********************************************************************************

clear all
set more off
set scheme s1color

***Set directory
capture cd "/Users/ykwang/Dropbox/Chinese food exports"
capture cd "/Users/Jie/Dropbox (Personal)/Chinese food exports"

***log file
cap log close
local date = c(current_date)
log using "Code/log files/firm_product_dataprep1_`date'.log", replace

***locals
local eventyear=2009

********************************
* Prepare main dataset: STEP 1 *
********************************

***load data

forvalues i=0/1{

use "Data/ChineseCustomsData/customs_yearly_2000_2013.dta", clear

ren city_id city_id2

keep if import==0 
drop unit_id shipment_id trans_id type_id2 interm 

g hs2digit=substr(hs_id,1,2)
g hs4digit=substr(hs_id,1,4)
g hs6=substr(hs_id,1,6)
destring hs6, replace

merge m:1 hs_id using "Data/product_codebook/HS_code_for_food", keepusing(hs_id)
drop if _m==2
g food=0 if _m==1
replace food=1 if _m==3
drop _m

merge m:1 hs_id using "Data/product_codebook/dairy_products_codebook", keepusing(hs_id)
drop if _m==2
g dairy=0 if _m==1
replace dairy=1 if _m==3
drop _m

if `i'==1{

  keep if dairy==1
  
  }
  
***Merge in the affectd firms/products/firm-products

merge m:1 party_id using "Data/inspection_lists/contaminated_firmlist_customs.dta"   //mark contaminated firms
tab _m
gen affected_firms=.
replace affected_firms=1 if _m==3
replace affected_firms=0 if _m==1
drop if _m==2
drop _m

merge m:1 hs_id using "Data/inspection_lists/contaminated_productlist_customs.dta", keepusing(hs_id)   //mark contaminated products
tab _m
gen affected_products=.
replace affected_products=1 if _m==3
replace affected_products=0 if _m==1
drop if _m==2
drop _m

merge m:1 party_id hs_id using "Data/inspection_lists/contaminated_list_full.dta", keepusing(party_id hs_id)
gen affected_firm_products=.
replace affected_firm_products=1 if _m==3
replace affected_firm_products=0 if _m==1
drop if _m==2
drop _m


***Merge in the innocent firms/products/firm-products

merge m:1 party_id using "Data/inspection_lists/innocnet_firmlist_customs.dta", keepusing(party_id)   //mark innocent firms
tab _m
gen innocent_firms=.
replace innocent_firms=1 if _m==3
replace innocent_firms=0 if _m==1
gen noninsp_firms=.
replace noninsp_firms=(innocent_firms==0 & affected_firms==0)
drop if _m==2
drop _m

merge m:1 party_id hs_id using "Data/inspection_lists/innocent_list_full.dta", keepusing(party_id hs_id)
gen innocent_firm_products=.
replace innocent_firm_products=1 if _m==3
replace innocent_firm_products=0 if _m==1
gen noninsp_firm_products=.
replace noninsp_firm_products=(innocent_firm_products==0 & affected_firm_products==0)
drop if _m==2
drop _m


***Merge in the baseline variables

merge m:1 party_id using "Data/Coded data/Customs/customs_firmchar_bl"
tab _m
drop if _m==2
drop _m

merge m:1 hs4digit using "Data/Coded data/Customs/customs_hs4exports_bl"
replace hs4avg_sales_bl=0 if _m==1
drop if _m==2
drop _m

merge m:1 hs2digit using "Data/Coded data/Customs/customs_hs2exports_bl"
replace hs2avg_sales_bl=0 if _m==1
drop if _m==2
drop _m


merge m:1 party_id using "Data/Coded data/Customs/customs_firmexports_bl"
replace firm_sales_bl=0 if _m==1
replace firm_exp_bl=0 if _m==1
drop if _m==2
drop _m


merge m:1 party_id hs_id using "Data/Coded data/Customs/customs_firmprodexports_bl"
replace firmprod_sales_bl=0 if _m==1
replace firmprod_exp_bl=0 if _m==1
drop if _m==2
drop _m

merge m:1 party_id using "Data/Coded data/Customs/customs_intensityofafirms_bl"
drop if _m==2
replace intensity_afirms=0 if intensity_afirms==.
drop _m

merge m:1 hs_id using "Data/Coded data/Customs/customs_intensityofaproducts_bl"
drop if _m==2
replace intensity_aproducts=0 if intensity_aproducts==.
drop _m

***Merge in export destination information

//countries with import bans
merge m:1 origin_id using "Data/country_codebook/ban_country_list"
tab _m
drop if _m==2
drop _m

//high income countries
merge m:1 origin_id using "Data/country_codebook/oecd_high_income_country_list.dta"   //mark oecd&high income countries
tab _m
replace oecdhighincome=0 if _m==1 
drop if _m==2
drop _m

***Merge in sourcing location information
merge m:1 city_id2 using "Data/country_codebook/sourcinglocation_id.dta"
tab _m
drop if _m==2
drop _m

***Merge in GTrends data
merge m:1 origin_id using "Data/google_trends/gtrends.dta"
tab _m
replace gwebindex=0 if gwebindex==.
replace gnewsindex=0 if gnewsindex==.
drop if _m==2
drop _m

***Merge in driving firms and destinations data
merge m:1 party_id using "Data/firm_codebook/driving_firms.dta"
gen driving_firms=.
replace driving_firms=1 if _m==3
replace driving_firms=0 if _m==1
drop _m

merge m:1 origin_id using "Data/country_codebook/driving_destinations.dta"
gen driving_destinations=.
replace driving_destinations=1 if _m==3
replace driving_destinations=0 if _m==1
drop _m

***Identify intermediaries and Manufacturers
merge m:1 party_id year using "Data/firm_codebook/company_codebook.dta"
drop if _m==2
drop _m
g interm = 1 if ustrpos(company,"进出口") != 0 | ustrpos(company,"经贸") != 0 | ustrpos(company,"贸易") != 0 | ustrpos(company,"科贸") != 0 | ustrpos(company,"外经") != 0
replace interm = 0 if interm==. & company!=""

replace round1=0 if round1==.
replace round2=0 if round2==.
replace round3=0 if round3==.
replace news=0 if news==.


***
replace origin_id=12 if origin_id==13
replace origin_id=24 if origin_id==25
replace origin_id=105 if origin_id==104
replace origin_id=118 if origin_id==119
replace origin_id=145 if origin_id==146
replace origin_id=155 if origin_id==156
replace origin_id=214 if origin_id==215
replace origin_id=260 if origin_id==257
replace origin_id=261 if origin_id==262

***

if `i'==0{

  save "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_step1",replace
  
  }

if `i'==1{

  save "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_step1",replace
  
  }
  

}

log close
